package study.linux.laptrinhlinux.mysql.source;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class QueryTime {
	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		String schema = "fulltexttest";
		String table = "articles";
		String[] fields = {"id", "title", "body"};
		int length = fields.length;

		long start;
		long end;

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String connectionUrl = "jdbc:mysql://localhost:3306/" + schema;
			String connectionUser = "testhost";
			String connectionPassword = "testhostdmg";
			conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
			stmt = conn.createStatement();


			// articles het 60, articles_title het 5
			start = System.currentTimeMillis();
//			rs = stmt.executeQuery("SELECT id,title,body FROM " + table + " where title like \"%my title%\"");
			rs = stmt.executeQuery("SELECT id,title,body FROM " + table + " where MATCH(title) AGAINST ('\"my title\"' IN BOOLEAN MODE)");
			end = System.currentTimeMillis();

			System.out.println("==time==\n" + (end-start) + "\n===");

			while (rs.next()) {
				for (int i = 0; i < length; i++) {
					String field = fields[i];
					String val = rs.getString(field);
					System.out.print(field + ": " + val + ", ");
				}
				System.out.println();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
			try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
			try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
		}
	}
}
